//
//  BaseViewController.m
//  ZoneTry
//
//  Created by Zonetry on 16/7/4.
//  Copyright © 2016年 ZoneTry. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
}


/**
 *  设置右侧按钮
 *
 *  @param image 图片imageString
 */
- (void) setRightButtomWithImage:(NSString *) image{
    
    // 添加解除绑定按钮
    UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    moreBtn.frame = CGRectMake(0, 0, 40, 40);
    [moreBtn setImage:[UIImage imageNamed:@"ui_menu"] forState:UIControlStateNormal];
    [moreBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:moreBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    if ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue] >=7 ) {
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        negativeSpacer.width = -10;
        self.navigationItem.rightBarButtonItems = @[negativeSpacer, rightItem];
    }
    
}

#pragma mark - 设置导航栏左侧按钮
- (void)setLeftBarButtonItemWithButton:(UIButton *)button andBool:(BOOL)Bool
{
    if (Bool == YES) {
        UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        if ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue] >=7 ) {
            UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            negativeSpacer.width = -20;
            self.navigationItem.leftBarButtonItems = @[negativeSpacer, buttonItem];
        }else{
            self.navigationItem.leftBarButtonItem = buttonItem;
        }
    } else {
        self.navigationItem.leftBarButtonItem = nil;
    }
}

#pragma mark - 设置返回按钮
- (void)setIsBackButton:(BOOL)isBackButton
{
    if (_isBackButton != isBackButton) {
        _isBackButton = isBackButton;
        
        // 创建返回按钮
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        backButton.frame = CGRectMake(0, 0, 50, 44);
        [backButton setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
        
        // 设置导航栏左侧按钮
        [self setLeftBarButtonItemWithButton:backButton andBool:isBackButton];
    }
}

#pragma mark - 设置关闭按钮
- (void)setIsDismissButton:(BOOL)isDismissButton
{
    if (_isDismissButton != isDismissButton) {
        _isDismissButton = isDismissButton;
        
        // 创建关闭按钮
        UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        closeButton.frame = CGRectMake(0, 0, 50, 44);
        [closeButton setImage:[UIImage imageNamed:@"CloseCommentView"] forState:UIControlStateNormal];
        [closeButton addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
        
        // 设置导航栏左侧按钮
        [self setLeftBarButtonItemWithButton:closeButton andBool:isDismissButton];
    }
}

/**
 *  设置导航栏标题透明度方法
 *
 *  @return
 */
- (void)setNavTitleBackgroundColorWithAlpha:(CGFloat)alpha
{
    [self.navigationController.navigationBar setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:
      [UIColor colorWithRed:37.0 / 255 green:31.0 / 255 blue:55.0 / 255 alpha:alpha], NSForegroundColorAttributeName,
      [UIFont fontWithName:@"Arial-Bold" size:16], NSFontAttributeName,
      nil]];
}
/**
 *  结束编辑退出软键盘
 *
 *  @return
 */
-(void) endEditing
{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self endEditing];
}

/**
 *  backAction返回按钮点击事件 pop
 *
 *  @return
 */
- (void)backAction:(UIButton *)button
{
    if (self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
/**
 *  关闭按钮 dismiss
 *
 *  @param button
 */
- (void)closeAction:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)rightBtnClick:(UIButton *)button {
    
}
/**
 *
 */
- (void) hidenNavBar{
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]];
    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
}
/**
 *  显示NavgationBar
 */
- (void) showNavBar{
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar lt_setBackgroundColor:kNavBackgroundColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
